<template>
  <div>
    <el-card>
      <div class="header">
        <el-button type="primary" @click="handleAdd">添加管理员</el-button>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="name"
          label="名称"
        >
        </el-table-column>
        <el-table-column
          prop="account"
          label="账号"
        >
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建日期"
        >
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <create ref="create" @Ok="handleInit"></create>
    <edit ref="edit" @Ok="handleInit"></edit>
  </div>
</template>

<script>
  import {deleteUser, getUserList} from "@/api/roleApi";
  import Create from './components/create'
  import Edit from './components/edit'

  export default {
    name: "index",
    components: {
      Create,
      Edit
    },
    data() {
      return {
        tableData: []
      }
    },
    mounted() {
      this.handleInit()
    },
    methods: {
      handleInit() {
        getUserList().then(res => {
          this.tableData = res.result
        })
      },
      handleAdd() {
        this.$refs['create'].show()
      },
      handleEdit(data) {
        this.$refs['edit'].show(data)
      },
      handleDelete(id) {
        deleteUser({id: id}).then(res => {
          if (res.status === 200) {
            this.$message.success("删除成功")
            this.handleInit()
          }
        })
      }
    }
  }
</script>

<style scoped>
  .header {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
</style>
